package com.qianjiang.module.lib_common.base;

import android.app.Application;

import com.lzy.okgo.model.HttpHeaders;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.qianjiang.module.common.R;
import com.qianjiang.module.common.utils.PreferenceUtil;
import com.qianjiang.module.common.utils.Utils;

import java.util.List;

/**
 * 要想使用BaseApplication，必须在组件中实现自己的Application，并且继承BaseApplication；
 * 组件中实现的Application必须在debug包中的AndroidManifest.xml中注册，否则无法使用；
 * 组件的Application需置于java/debug文件夹中，不得放于主代码；
 * 组件中获取Context的方法必须为:Utils.getContext()，不允许其他写法；
 *
 * @author 2016/12/2 17:02
 * @version V1.0.0
 * @name BaseApplication
 */
public class BaseApplication extends Application {

    public static final String ROOT_PACKAGE = "com.qianjiang.module";

    private static BaseApplication sInstance;
    private String saas_proappcode = "";
    private String saas_tenantcode = "";
    private String ServerUrl = "";

    public String getServerUrl(){
        return  getString(R.string.environment).equals("production") ?
                "http://app.paas.com" :
                "http://uata.paas.qjclouds.com";
    }

    public String getCookie_name() {
        return cookie_name;
    }

    public void setCookie_name(String cookie_name) {
        this.cookie_name = cookie_name;
    }

    public String getCookie_value() {
        return cookie_value;
    }

    public void setCookie_value(String cookie_value) {
        this.cookie_value = cookie_value;
    }

    private String cookie_name = "";
    private String cookie_value = "";
    private String token = "";

    public HttpHeaders getHeaders() {
        headers.put("User-Agent", "qj-app");
        if (!getSaas_proappcode().isEmpty()) {
            headers.put("saas-proappcode", getSaas_proappcode());
        }
        if (!getSaas_tenantcode().isEmpty()){
            headers.put("saas-tenantcode", getSaas_tenantcode());
        }
        String token_name = PreferenceUtil.getStringValue("token_name","",this);
        String token_value = PreferenceUtil.getStringValue("token_value","",this);
        if(!token_name.isEmpty()
                &&!token_value.isEmpty()){
            headers.put(token_name, token_value);
        }
        return headers;
    }

    private HttpHeaders headers = new HttpHeaders();

    private List<com.qianjiang.module.common.base.IApplicationDelegate> mAppDelegateList;


    public static BaseApplication getInstance() {
        if (null == sInstance) {
            sInstance = new BaseApplication();
        }
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        Logger.init("pattern").logLevel(LogLevel.FULL);
        Utils.init(this);
        mAppDelegateList = com.qianjiang.module.common.base.ClassUtils.getObjectsWithInterface(this, com.qianjiang.module.common.base.IApplicationDelegate.class, ROOT_PACKAGE);
        for (com.qianjiang.module.common.base.IApplicationDelegate delegate : mAppDelegateList) {
            delegate.onCreate();
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        for (com.qianjiang.module.common.base.IApplicationDelegate delegate : mAppDelegateList) {
            delegate.onTerminate();
        }
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
        for (com.qianjiang.module.common.base.IApplicationDelegate delegate : mAppDelegateList) {
            delegate.onLowMemory();
        }
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        for (com.qianjiang.module.common.base.IApplicationDelegate delegate : mAppDelegateList) {
            delegate.onTrimMemory(level);
        }
    }


    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getSaas_proappcode() {
        return saas_proappcode;
    }

    public void setSaas_proappcode(String saas_proappcode) {
        this.saas_proappcode = saas_proappcode;
    }

    public String getSaas_tenantcode() {
        return saas_tenantcode;
    }

    public void setSaas_tenantcode(String saas_tenantcode) {
        this.saas_tenantcode = saas_tenantcode;
    }
}
